//
//  RegistLastViewController.m
//  QRMaster
//
//  Created by myApple on 15/3/21.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "RegistLastViewController.h"
#import "RequestNetWork.h"
#import "PromptInfoView.h"
#import "StartPageView.h"
#import "RegistView.h"
#import "StartPageController.h"
#import "Reachability.h"
#import "validateInputValue.h"
#import "QRSingleton.h"

@interface RegistLastViewController ()<RegistViewDelegate>

@end

@implementation RegistLastViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self prepareTitleView];
    self.title = NSLocalizedString(@"writeCheckNum", nil);
    [self prepareView];
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)prepareTitleView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = 20;
    float y = 10;
    float width = ScreenWidth - 2 * x;
    float height = 20;
    
    UILabel * promtLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    promtLabel.text = NSLocalizedString(@"sendCheckNum", nil);
    promtLabel.textAlignment = NSTextAlignmentCenter;
    promtLabel.font = [UIFont systemFontOfSize:12.0];
    [self.view addSubview:promtLabel];
    
    
    NSMutableAttributedString * astr = [[NSMutableAttributedString alloc]initWithString:promtLabel.text];
    [astr addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12.0],NSFontAttributeName,RGBA(135, 199, 66, 1),NSForegroundColorAttributeName, nil] range:[promtLabel.text rangeOfString:NSLocalizedString(@"checkNum", nil)]];
    promtLabel.attributedText = astr;
    
    
    y += height + 20;
    width = ScreenWidth - 2 * x;
    height = 20;
    UILabel * telePhoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(x,y,width, height)];
    if([validateInputValue isPureNumandCharacters:_mobileNumber]==YES)
    {
        telePhoneLabel.text = [NSString stringWithFormat:@"+%@%@",_areaCode,_mobileNumber];
    }
    else
    {
        telePhoneLabel.text = [NSString stringWithFormat:@"%@",_mobileNumber];
    }
    telePhoneLabel.textAlignment = NSTextAlignmentCenter;
    telePhoneLabel.font = [UIFont boldSystemFontOfSize:14.0];
    [self.view addSubview:telePhoneLabel];
    
}


-(void)prepareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 50;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    RegistView * rv = [[RegistView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    rv.delegate = self;
    rv.mobile = _mobileNumber;
    [self.view addSubview:rv];
}

-(void)getData:(NSString *)checkCode registerWay:(NSString *)registerWay
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    if ([@"0" isEqualToString:registerWay]==YES) {
        [postDict setValue:_mobileNumber forKey:@"mobile"];
        [postDict setValue:@"" forKey:@"email"];
        [postDict setValue:_areaCode forKey:@"areaCode"];
    }
    else
    {
        [postDict setValue:@"" forKey:@"mobile"];
        [postDict setValue:_mobileNumber forKey:@"email"];
    }
    [postDict setValue:_passWord forKey:@"passwd"];
    [postDict setValue:checkCode forKey:@"authCode"];
    [rnk initWithPath:POST(REGISTEREND) params:postDict block:^(id downLoadData, id error) {
        [self stopWaiting];
        if ([downLoadData objectForKey:@"data"] != nil) {
            [PromptInfoView showMessage:NSLocalizedString(@"registSuccess", nil)];
            NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
            [userInfo setObject:_mobileNumber forKey:@"userMobile"];
            [userInfo setObject:_areaCode forKey:@"areaCode"];
            [userInfo synchronize];
            [QRSingleton sharedQRSingleton].loginViewShow = YES;
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        else  {
            if ([[error objectForKey:@"status"]integerValue] == 213)
                
            {
                [PromptInfoView showMessage:NSLocalizedString(@"errorVcode", nil) superView:self.view];
            }
            else
            {
                [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
            }
        }
   
    }];
}

#pragma mark ---- registLastStepDelegate

-(void)LastStepView:(NSString *)checkVode registerWay:(NSString *)registerWay
{
    [self getData:checkVode registerWay:registerWay];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"MakeSureRegister" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
